package com.java.dp.proxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Author lizhilong
 * @create 2019/12/3 14:37
 * @desc
 */
public class CGLibProxy implements MethodInterceptor {

    private Object object;

    public Object getPorxyInstance (Object object){
        this.object = object;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(this.object.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        beforeBuy();
        Object result =  methodProxy.invokeSuper(o,objects);
        afterBuy();
        return result;
    }

    private void  beforeBuy(){
        System.out.println("购买火车票之前需要在携程app预约");
    }

    private  void  afterBuy(){
        System.out.println("购买成功要支付手续费");
    }
}
